$('.company_name').on('click',function(){
	opsearch('{:url("html/system/company")}?op=search')
})

var brand=[];
var model=[];
var series=[];
//请求品牌
ajax(api.car.brands, {
	parent_id: 0
}, 'post').done(function(res) {
	console.log(res)
	brand=res.data
	
	{if $Think.get.car_id}
	brandselect('car_brand_id',res.data,cardetail.data[0].car_brand_id)
	{else/}
	brandselect('car_brand_id',res.data)
	if(brand.length>0){
	ajax(api.car.brands, {
		parent_id:brand[0].car_brand_id
	}, 'post').done(function(res2) {
		model=res2.data
		brandselect('car_model_id',res2.data)
		if(model.length>0){
			ajax(api.car.brands, {
				parent_id: model[0].car_brand_id
			}, 'post').done(function(res3) {
				series=res3.data
				brandselect('car_series_id',res3.data)
			}).fail()
		}
	}).fail()
	}
	{/if}
}).fail()
{if $Think.get.car_id}
	ajax(api.car.brands, {
		parent_id: cardetail.data[0].car_brand_id
	}, 'post').done(function(res2) {
		series=res2.data
		brandselect('car_model_id',res2.data,cardetail.data[0].car_model_id)
	}).fail()
	ajax(api.car.brands, {
		parent_id: cardetail.data[0].car_model_id
	}, 'post').done(function(res3) {
		series=res3.data
		brandselect('car_series_id',res3.data,cardetail.data[0].car_series_id)
	}).fail()
}).fail(function(){})
{else/}
{/if}



form.on('select(car_brand_id)', function(data) {
	Load()
	ajax(api.car.brands, {
		parent_id:data.value
	}, 'post').done(function(res2) {
		model=res2.data
		brandselect('car_model_id',res2.data)
		if(model.length>0){
			ajax(api.car.brands, {
				parent_id: model[0].car_brand_id
			}, 'post').done(function(res3) {
				if(res3.code==1){
				Load(1)
				series=res3.data
				brandselect('car_series_id',res3.data)
				}else{
					brandselect('car_series_id',[])
				}
			}).fail()
		}else{
			brandselect('car_series_id',res2.data)
			Load(1)
		}
	}).fail()
})
form.on('select(car_model_id)', function(data) {
	Load()
	ajax(api.car.brands, {
				parent_id:data.value
			}, 'post').done(function(res3) {
				if(res3.code==1){
					Load(1)
					series=res3.data
					brandselect('car_series_id',res3.data)
				}else{
					brandselect('car_series_id',[])
				}
			}).fail()
})


function brandselect(classname,res,id){
	var options=``
	if(res.length>0){
		for(var i=0;i<res.length;i++){
			if(id){
				if(res[i].car_brand_id==id){
					options+=`<option selected value='${res[i].car_brand_id}'>${res[i].name}</option>`
				}else{
					options+=`<option value='${res[i].car_brand_id}'>${res[i].name}</option>`
				}
			}else{
				options+=`<option value='${res[i].car_brand_id}'>${res[i].name}</option>`
			}
			
		}
	}else{
		options=`<option selected value='0'>无数据</option>`	
	}
	$("."+classname).html(options)
	form.render('select');
}
form.on('submit(add)', function(data){
	Load()
  console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
  console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
  console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
  ajax(api.cycle_buy.add,data.field,'post').done(function(res){
  	Load(1)
  	if(res.code==1){
  		Msg('添加成功');
  		window.location.href='{:url("html/car/audit")}'
  	}else{
  		Err(res.msg)
  	}
  }).fail(function(){
  		Load(1)
  		Err('请求失败')
  })
  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
});


